package com.zzyl.nursing.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.CreateReservationDto;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeSlotAvailabilityVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询当天取消预约的数据量
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        //调用服务层实现查询取消预约数量
        int cancelledCount = reservationService.cancelCount();
        System.out.println("------" + userId);
        return R.ok(cancelledCount);
    }

    /**
     * 取消预约
     */
    @ApiOperation("取消预约")
    @PostMapping("/{id}/cancel")
    public R cancel(@ApiParam("预约信息ID") @RequestBody Long id) {
        reservationService.cancel(id);
        return R.ok();
    }

    /**
     * 查询预约信息列表
     */
    @ApiOperation("查询预约信息列表")
//    @PreAuthorize("@ss.hasPermi('nursing:reservation:list')")
    @GetMapping("/page")
    public TableDataInfo<List<Reservation>> list(@ApiParam("预约信息查询条件") Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }



    /**
     * 新增预约信息
     */
    @ApiOperation("新增预约信息")
//    @PreAuthorize("@ss.hasPermi('nursing:reservation:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("预约信息信息") @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }
//    @ApiOperation("新增预约")
//    @PostMapping
//    @Log(title = "预约信息", businessType = BusinessType.INSERT)
////    @PreAuthorize("@ss.hasPermi('member:reservation:add')")
//    public R<Void> createReservation(
//            @RequestBody @Validated CreateReservationDto dto) {
//        reservationService.createReservation(dto);
//        return R.ok();
//    }

    /**
     * 查询每个时间段剩余预约次数
     * @param timestamp
     * @return
     */
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public R<List<TimeSlotAvailabilityVo>> countByTime(
            @ApiParam(value = "时间戳", required = true, example = "1726704000000")
            @RequestParam("time") Long timestamp) {

        List<TimeSlotAvailabilityVo> result = reservationService.countAvailabilityByTime(timestamp);
        return R.ok(result);
    }
}